/*
 * Filename:    WarnApprovalResultController.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年11月13日
 */
package com.paic.mhis.hcpms.warning.web.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.Authority;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.common.workflow.vo.HcpmsWorkFlowVO;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityMedicalService;
import com.paic.mhis.hcpms.warning.biz.service.HcpmsWarnService;
import com.paic.mhis.hcpms.warning.biz.service.WarningApprovalResultService;
import com.paic.mhis.hcpms.warning.biz.service.WarningAuditResultService;
import com.paic.mhis.hcpms.warning.biz.service.WarningExamineResultService;
import com.paic.mhis.hcpms.warning.biz.service.WarningWorkFlowService;
import com.paic.mhis.hcpms.warning.dto.HcpmsWarningDTO;
import com.paic.mhis.hcpms.warning.dto.WarningApprovalResultDTO;
import com.paic.mhis.hcpms.warning.dto.WarningAuditResultDTO;
import com.paic.mhis.hcpms.warning.dto.WarningResultAuditDTO;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.exceptions.BusinessException;

@Controller
@RequestMapping("/warnApprovalResult/")
public class WarnApprovalResultController {
	@BizServicesProxy
	private WarningAuditResultService warnAuditResultService;
	@BizServicesProxy
	private WarningExamineResultService warnExamineResultService;
	@BizServicesProxy
	private WarningApprovalResultService warnApprovalResultService;
	@BizServicesProxy
	private WarningWorkFlowService warningWorkFlowService;
	@BizServicesProxy
	private HcpmsWarnService hcpmsWarnService;

	@BizServicesProxy(beanName = "integrityMedicalService")
	private IntegrityMedicalService integrityMedicalService;

	@RequestMapping("warnApprovalResult.do")
	@ResponseBody
	public Map<String, Object> warnApprovalResult(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		String warnNo = request.getParameter("warnNo");
		paramMap.put("warnno", StringUtils.isEmpty(warnNo) ? null : warnNo);
		List<WarningAuditResultDTO> auditResultList = warnAuditResultService.getAuditResultData(paramMap);
		List<WarningResultAuditDTO> examineResultList = warnExamineResultService.getExamineResultData(paramMap);
		List<WarningApprovalResultDTO> approvalResultList = warnApprovalResultService.getApprovalResultData(paramMap);

		if (auditResultList.size() > 0) {
			WarningAuditResultDTO auditResult = auditResultList.get(0);
			map.put("auditResult", auditResult);
		}
		if (examineResultList.size() > 0) {
			WarningResultAuditDTO examineResult = examineResultList.get(0);
			map.put("examineResult", examineResult);
		}
		if (approvalResultList.size() > 0) {
			WarningApprovalResultDTO approvalResult = approvalResultList.get(0);
			map.put("approvalResult", approvalResult);
		}
		return map;
	}

	@RequestMapping(value = "saveApprovalResult.do", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveApprovalResult(HttpServletRequest request) throws UnsupportedEncodingException,
			IOException, BusinessException {
		UserDetails user = (UserDetails) request.getSession().getAttribute("user");
		String str = HttpProcessUtil.readRequestBody(request);
		JSONObject jarray = JSON.parseObject(str);
		integrityMedicalService.addIntegrityPointTemps(jarray, user.getUsername());// 临时扣分
		Map<String, Object> map = new HashMap<String, Object>();
		String warnNo = jarray.get("warnNo").toString();
		String approveOpinion = jarray.get("approveOpinion").toString();
		String approveDesc = jarray.get("approveDesc").toString();
		String username = user.getUsername();
		WarningApprovalResultDTO approvalResultDTO = new WarningApprovalResultDTO();
		approvalResultDTO.setWarnNo(warnNo);
		approvalResultDTO.setApprover(StringUtils.isEmpty(username) ? "" : username);
		approvalResultDTO.setApproveDesc(approveDesc);
		approvalResultDTO.setCodeDesc(approveOpinion);
		try {
			int result = warnApprovalResultService.updateApprovalResultData(approvalResultDTO);
			if (result > 0) {
				// 获取参数
				HcpmsWorkFlowVO wfvo = new HcpmsWorkFlowVO();
				List<Authority> authorityList = user.getAuthorities();
				List<String> role = new ArrayList<String>();
				for (Authority authority : authorityList) {
					role.add(authority.getAuthority());
				}
				wfvo.setRole(role);
				wfvo.setTaskNo(warnNo);
				wfvo.setActCurrent(user.getUsername());
				Map<String, String> condition = new HashMap<String, String>();
				condition.put("AUDIT_OPINION", approveOpinion);
				wfvo.setParamValue(condition);
				// 启动
				String actCurrent = warningWorkFlowService.processWorkFlow(wfvo);
				// 更新状态
				HcpmsWarningDTO warnDto = new HcpmsWarningDTO();
				warnDto.setWarnNo(warnNo);
				warnDto.setStatus(actCurrent);
				hcpmsWarnService.updateHcpmsWarn(warnDto);
				
				//流程结束 扣分信息转移
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("warnNo", warnNo);
				paramMap.put("userName", user.getUsername());
				integrityMedicalService.addIntegrityPointTemps(paramMap);
				map.put("result", true);
				map.put("msg", "操作成功");
			} else {
				map.put("result", false);
				map.put("msg", "操作失败");
			}
		} catch (Exception e) {
			map.put("result", false);
			map.put("msg", "操作失败");
		}
		return map;
	}
}
